﻿/***
	
	# CopyRights By Pr (yslee96@naver.com / webmaster@prev.kr) 
		
	@ Author Pr (yslee96@naver.com) 
	
	@ http://prev.kr
	
***/

package kr.prev.swe {
	
	import flash.utils.ByteArray;
	import flash.display.Loader;
	import flash.system.LoaderContext;
	import flash.events.IEventDispatcher;
	import flash.filesystem.File;
	import flash.filesystem.FileStream;
	import flash.filesystem.FileMode;
	import flash.display.NativeWindow;
	import flash.display.NativeWindowInitOptions;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	
	[Event(name = "loadStart",type = "kr.prev.swe.SWEPlayerEvent")]
	
	[Event(name = "loadInit",type = "kr.prev.swe.SWEPlayerEvent")]
	
	[Event(name = "closing",type = "kr.prev.swe.SWEPlayerEvent")]
	
	[Event(name = "formatError",type = "kr.prev.swe.SWEPlayerErrorEvent")]
	
	[Event(name = "swfError",type = "kr.prev.swe.SWEPlayerErrorEvent")]
	
	public class SWEPlayer implements IEventDispatcher {
		
		private var _SWFLoader:Loader 
		private var _swe:SWE
		private var _nativeWindow:NativeWindow
		
		
		public function get SWFLoader():Loader {
			return _SWFLoader
		}
		public function get swe():SWE {
			return _swe
		}
		public function get nativeWindow():NativeWindow {
			return _nativeWindow
		}
		
		public function SWEPlayer(bytes:ByteArray=null) {
			if (bytes) {
				_swe = new SWE(bytes)
			}
		}
		
		public function analyzeByFile(file:File):void {
			var tempFileStream:FileStream = new FileStream()
			tempFileStream.open(file, FileMode.READ)
			var tempBytes:ByteArray = new ByteArray()
			tempFileStream.readBytes(tempBytes, 0, tempFileStream.bytesAvailable)
			
			analyze(tempBytes)
		}
		
		public function analyze(bytes:ByteArray):void {
			_swe = new SWE(bytes)
			
			var $bytes:ByteArray = new ByteArray()
			bytes.position = 0
			bytes.readBytes($bytes, 0, bytes.length)
			
			_swe.analyze($bytes)
			
			if (!_swe.getFrame( SWE.CONTENT_SWF_DATA ) ) {
				dispatchEvent( new SWEPlayerErrorEvent(SWEPlayerErrorEvent.FORMAT_ERROR, 'NO-SWF-DATA') )
			}
		}
		
		public function createWindow():void {
			var _nativeWindowOption:NativeWindowInitOptions = new NativeWindowInitOptions()
			_nativeWindowOption.maximizable = false
			_nativeWindowOption.resizable = false
			
			_nativeWindow = new NativeWindow(_nativeWindowOption)
			_nativeWindow.stage.align = StageAlign.TOP_LEFT
			_nativeWindow.stage.scaleMode = StageScaleMode.NO_SCALE
		}
		
		public function load():void {
			if (!_swe.getFrame( SWE.CONTENT_SWF_DATA ) ) {
				dispatchEvent( new SWEPlayerErrorEvent(SWEPlayerErrorEvent.SWF_ERROR, 'NO-SWF-DATA' ) )
				return
			}
			
			_SWFLoader = new Loader()
			
			_nativeWindow.stage.addChild( _SWFLoader )
			
			var SWFLoaderContext:LoaderContext = new LoaderContext()
			SWFLoaderContext.allowCodeImport = true
		
			_SWFLoader.loadBytes( _swe.getFrame( SWE.CONTENT_SWF_DATA).bytes , SWFLoaderContext )
			_SWFLoader.contentLoaderInfo.addEventListener(Event.INIT, loadInitHandler)
			_nativeWindow.addEventListener( Event.CLOSING, nativeWindowClosingHandler )
			
			this.dispatchEvent( new SWEPlayerEvent(SWEPlayerEvent.LOAD_START, _SWFLoader, _nativeWindow) )
			
			return
		}
		
		
		private function loadInitHandler(e:Event):void {
			this.dispatchEvent( new SWEPlayerEvent(SWEPlayerEvent.LOAD_INIT, _SWFLoader, _nativeWindow) )
			//_SWFLoader.contentLoaderInfo.removeEventListener(Event.INIT, loadInitHandler)
		}
		
		
		private function nativeWindowClosingHandler(e:Event) {
			this.dispatchEvent( new SWEPlayerEvent(SWEPlayerEvent.CLOSING, _SWFLoader, _nativeWindow) )
		}
		
		
		include "../source/IEventDispatcher.as"
		
		
	}
	
}